package com.aepryus.loom;

import java.util.*;
import com.aepryus.util.*;

public abstract class Job extends Thread {
	private long jobID;
	private Date startTime;
	private Date endTime;
	private String username;
	private Throwable throwable;

	private boolean oked;

	public abstract String getStatus();
	protected abstract void process() throws PersistenceException,ValidationException;

	public Job () {}

	void setJobID (long jobID) {this.jobID = jobID;}
	public long getJobID () {return jobID;}
	public Date getStartTime () {return startTime;}
	public Date getEndTime () {return endTime;}
	void setUsername (String username) {this.username = username;}
	public String getUsername () {return username;}
	public Throwable getThrowable () {return throwable;}

	void ok () {
		oked = true;
	}

	public void run () {
		if (!oked) throw new RuntimeException("Jobs can only be run by submitting them to the Runner.  Use Runner.submit(job).");
		try {
		    startTime = new Date();
		    process();
		    endTime = new Date();
		} catch (Throwable t) {
		    throwable = t;
		    Log.print(t);
		}
		Runner.complete(this);
	}
}

